package fr.vahren.dotastrat.gui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;

import fr.vahren.dotastrat.game.D2SGame;
import fr.vahren.dotastrat.game.D2SMessage;
import fr.vahren.dotastrat.game.D2SMessageType;
import fr.vahren.dotastrat.model.D2SHeroUnit;
import fr.vahren.dotastrat.model.D2SZone;

public class D2SSelectionInfoWidget extends D2SDockWidget {

	private static Map<D2SMessageType, QColor> messageTypeColorMap = new HashMap<>();
	static {
		messageTypeColorMap.put(D2SMessageType.GENERAL, QColor.black);
		messageTypeColorMap.put(D2SMessageType.ATTACK, QColor.darkRed);
		messageTypeColorMap.put(D2SMessageType.ABILITY, QColor.darkBlue);
		messageTypeColorMap.put(D2SMessageType.DEATH, QColor.red);
		messageTypeColorMap.put(D2SMessageType.LEVELUP, QColor.cyan);
		messageTypeColorMap.put(D2SMessageType.RESPAWN, QColor.green);
		messageTypeColorMap.put(D2SMessageType.MOVE, QColor.gray);
		messageTypeColorMap.put(D2SMessageType.BUY, QColor.yellow);
	}

	private QLabel generalInfo;
	private List<D2SZoneInfoWidget> infos = new ArrayList<>();
	private QWidget mainWidget;
	private QWidget infoWidget;
	private D2SConsole consoleWidget;

	public D2SSelectionInfoWidget() {
		mainWidget = new QWidget();
		mainWidget.setLayout(new QVBoxLayout());
		setWidget(mainWidget);
		infoWidget = new QWidget();
		infoWidget.setLayout(new QVBoxLayout());
		generalInfo = new D2SLabel("...", "grey", 10);
		generalInfo.setWordWrap(true);
		consoleWidget = new D2SConsole();
		infoWidget.layout().addWidget(generalInfo);
		mainWidget.layout().addWidget(infoWidget);
		mainWidget.layout().addWidget(consoleWidget);
	}

	@Override
	public void writeTextToStatus() {
		// TODO Auto-generated method stub

	}

	public void displayZoneInfo(int zoneId, D2SGame model) {
		// clear
		mainWidget = new QWidget();
		infoWidget = new QWidget();
		mainWidget.setLayout(new QVBoxLayout());
		infoWidget.setLayout(new QVBoxLayout());
		setWidget(mainWidget);
		mainWidget.layout().addWidget(infoWidget);
		mainWidget.layout().addWidget(consoleWidget);

		D2SZone zone = model.getMap().getZone(zoneId);
		generalInfo.setText(buildZoneInfo(zone));
		infoWidget.layout().addWidget(generalInfo);

		for (D2SZoneInfoWidget w : infos) {
			infoWidget.layout().removeWidget(w);
		}
		infos.clear();
		for (D2SHeroUnit unit : zone.getUnits()) {
			D2SZoneInfoWidget w = buildUnitInfo(unit);
			infos.add(w);
			infoWidget.layout().addWidget(w);
		}
	}

	private D2SZoneInfoWidget buildUnitInfo(D2SHeroUnit unit) {
		return new D2SZoneInfoWidget(unit);
	}

	private String buildZoneInfo(D2SZone zone) {
		StringBuffer info = new StringBuffer();

		info.append(zone.toString());
		info.append("\n");

		return info.toString();
	}

	public void updateConsole(D2SMessage message) {
		consoleWidget.appendLine(message.getText(),
				messageTypeColorMap.get(message.getType()));
	}

}
